import { datas } from './fields.js'
let that
// 串口监听
export default {
	data() {
		return {
			...datas,
			images: [],
			code: "",
			userInfo: {
				userName: '',
				roleCode: ''
			},
			infosss: '',
		}
	},
	computed: {},
	// 订单状态(0-借用待审批，1-借用审批驳回，2-借用审批通过，3-借用中，4-借用中(逾期)，5-归还待审批，6-归还审批驳回，7-归还审批通过，8-已归还，9-已归还(逾期)，10-报废，11-取消)
	onShow() {
		that = this
	},
	created() {
		that = this
	},
	onReady() {},
	onLoad(options) {
		that = this
		// 获取code
		that.code = options.code
		if (options.code) {
			uni.request({
				url: '/api/h5/login',
				method: "POST",
				header: {},
				data: { "code": options.code },
				success: (res) => {
					console.log(res)
					that.infosss = res.data
					console.log(res)
					console.log(res.data.data)
					console.log(res.data)
					if(res.data.code == 200) {
						uni.setStorageSync("userId", res.data.data.id)
						uni.setStorageSync("userInfo", res.data.data)
						that.userInfo = res.data.data
					} else {
						that.userInfo = {}
						uni.showToast({
							title: "登录失败",
							icon: 'none',
							duration: 1000,
						})
					}
					
					
				},
				fail: (err) => {
					console.error(err);
				}
			});
		}
	},
	methods: {
		changeUser(userType){
			if(userType == 0){
				uni.setStorageSync("userId", "1875114943158816768")
				that.userInfo.userName = '刘广威'
				that.userInfo.roleCode = 'user'
			}else{
				uni.setStorageSync("userId", "1740249226333523968")
				that.userInfo.userName = '测试管理员'
				that.userInfo.roleCode = 'admin'
			}
		},
		gotoLogin() {
			window.open(
				"https://xxcapp.xidian.edu.cn/uc/api/oauth/index?redirect=https://eme-lab.xidian.edu.cn/h5/pages/home/index&appid=200210726150337833&state=STATE&qrcode=1",
				'_self')
		},
		gotoPath(path) {
			uni.navigateTo({
				url: path,
				animationType: 'pop-in',
				animationDuration: 300
			})
		},
		getUrlValue(value) {
			// 从第一个?开始，且不包括#之后，并截取掉?的部分
			var query = location.search.substring(1);
			// 从#开始的部分，并转换成数组
			var hash = location.hash.split("?");
			// query和hash均没有参数
			if (!query && hash.length < 2) {
				return ''
			}
			// 先取query部分的参数进行匹配
			var vars = query.split("&")
			for (var i = 0; i < vars.length; i++) {
				var pair = vars[i].split("=")
				if (pair[0] == value) {
					return pair[1]
				}
			}
			// query没有参数，或者有参数但没找到，则取hash部分的参数
			if (!hash[1]) {
				return ''
			}
			vars = hash[1].split("&")
			for (var i = 0; i < vars.length; i++) {
				var pair = vars[i].split("=")
				if (pair[0] == value) {
					return pair[1]
				}
			}
			return ''
		}
	}
}